﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Etapa1;
using PloobsEngine.SceneControl;
using PloobsEngine;
using PloobsEngine.Input;
using PloobsEngine.Commands;
using PloobsEngine.Physics.XJigLib;
using PloobsEngine.Utils;
using PloobsEngine.Factories;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using PloobsEngine.Light;

namespace PloobsEngineDemo3
{
    public class LightThrow
    {
        IWorld _mundo;        
        Manager man;        
        EngineStuff engine;        
        Random rd = new Random();
        SleepTimer st;
        Vector3 pos;
        float dispersion;
        float velocityModuler = 50;

        public LightThrow(IWorld mundo, EngineStuff engine,Vector3 position, int interval, float dispersion,float velocityModuler)
        {
            pos = position;
            man = engine.Manager;
            _mundo = mundo;
            this.velocityModuler = velocityModuler;
            this.engine = engine;
            this.dispersion = dispersion;
            st = new SleepTimer(engine, interval, OnTimer);

        }

        public void OnTimer()
        {
            Vector3 velo = Vector3.Down;

            ///Create an object
            IObject physObj = SpawnPrimitive(pos, Matrix.Identity);
            velo.X = (float)  (rd.NextDouble()- 0.5f)/dispersion ;
            velo.Z = (float) (rd.NextDouble() - 0.5f )/ dispersion;
            physObj.PhysicObject.Velocity = velo * velocityModuler;

            ///Create a light that follow an object
            MoveablePointLight mvp = new MoveablePointLight(physObj.PhysicObject as SphereObject, new Color((float)rd.NextDouble(), (float)rd.NextDouble(), (float)rd.NextDouble()), 50, 5);

            ///Add them to the world
            _mundo.AddLight(mvp);
            _mundo.AddObject(physObj);
        }
        
        public LightThrow(IWorld mundo ,EngineStuff engine)
        {  
             man = engine.Manager;
             _mundo = mundo;
             this.engine = engine;          
            {
            ///Register a function to be called when the the mouse is pressed
            InputPlaybleMouseBottom ip1 = new SimpleConcreteMouseBottomInputPlayable(StateKey.PRESS, EntityType.IOBJECT, MouseButtons.LeftButton, mousebuttonteste);
            BindMouseCommand mm = new BindMouseCommand(ip1, BindAction.ADD);
            CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm);
            }

            {
                InputPlaybleMouseBottom ip = new SimpleConcreteMouseBottomInputPlayable(StateKey.PRESS, EntityType.IOBJECT, MouseButtons.RightButton, mousebuttontesteRight);
                BindMouseCommand mm = new BindMouseCommand(ip, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm);
            }


        }

        public void mousebuttontesteRight(MouseState ms)
        {
            PointLight pl = new PointLight(_mundo.ActiveCamera.Position, StaticRandom.RandomColor(), 100, 5);
            pl.UsePointLightQuadraticAttenuation = true;
            _mundo.AddLight(pl);
        }

        public void mousebuttonteste(MouseState ms)
        {
            ///Create an object
            IObject physObj = SpawnPrimitive(_mundo.ActiveCamera.Position, Matrix.CreateRotationX(0.5f));
            physObj.PhysicObject.Velocity = (_mundo.ActiveCamera.Target - _mundo.ActiveCamera.Position) * 50.0f;
            
            ///Create a light that follow an object
            MoveablePointLight mvp = new MoveablePointLight(physObj.PhysicObject as SphereObject, new Color((float)rd.NextDouble(),(float) rd.NextDouble(),(float) rd.NextDouble()),25, 5);
            mvp.UsePointLightQuadraticAttenuation = true;
            ///Add them to the world
            _mundo.AddLight(mvp);
            _mundo.AddObject(physObj);
      
        }
        /// <summary>
        /// Create a simple Sphere object
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="ori"></param>
        /// <returns></returns>
        private IObject SpawnPrimitive(Vector3 pos, Matrix ori)
        {
            ///Load a Model with a custom texture
            SimpleModel sm2 = new SimpleModel("..\\Content\\Model\\ball", "..\\Content\\Textures\\white");            
            sm2.LoadModelo();
            CustomDeferred nd = new CustomDeferred(false,false,false,false);            
            nd.Initialize();
            IMaterial m = new Material(nd,MaterialType.DEFERRED);            
            SphereObject  pi2 = new SphereObject(sm2, pos, ori,1f);
            pi2.Mass = 150;
            pi2.isMotionLess = false;
            IObject o = new SimpleObject(m, pi2, sm2);
            return o;
        }

    }
}
